﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Rison.DataAccess.Data.ZJCP;
using System.Text;

namespace Rison.Web.ZJCP.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class LoadComments : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string pageNum = context.Request.Params["pageNum"];
            if (String.IsNullOrEmpty(pageNum))
            {
                pageNum = "1";
            }
            int intPageNum = 1;
            try
            {
                intPageNum = int.Parse(pageNum);
            }
            catch(Exception e)
            {
            }

            int totalNum = CountTotal();
            string rv = "";
            if ((50 * (intPageNum - 1)) >= totalNum)
            {
                rv = "{success:false,code:1}";
            }
            else if (intPageNum <= 0)
            {
                rv = "{success:false,code:2}";
            }
            else
            {
                rv = String.Format("{{success:true,result:{0},totalPageNum:{1}}}", LoadComment(intPageNum), (totalNum/50) + 1);
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(rv);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string LoadComment(int pageNum)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" SELECT     TOP 50 *");
            sql.Append(" FROM         ViewDepartComment");
            sql.Append(" WHERE     (co_id NOT IN");
            sql.Append(String.Format("                           (SELECT     TOP {0} co_id", 50 * (pageNum - 1)));
            sql.Append("                             FROM          ViewDepartComment");
            sql.Append("                             ORDER BY co_id))");
            sql.Append(" ORDER BY co_id");
            DataSet ds = DbAccess.executeQuery(sql.ToString());
            return JsonCommon.ds2json(ds);
        }

        private int CountTotal()
        {
            String sql = "select count(1) from ViewDepartComment";
            DataSet ds = DbAccess.executeQuery(sql);
            return (int)(ds.Tables[0].Rows[0][0]);
        }
    }
}
